package com.topfun.vo;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.topfun.utils.Constants;
import com.topfun.utils.DateUtil;
import com.topfun.utils.MathUtils;
import com.topfun.utils.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by huangda on 2017/8/16.
 * 投注
 */
public class BetOrder {
    // ===========================================================
    // Constants
    // ===========================================================
    public static final int NOT_WINNING = 0;
    public static final int FIRST_WINNING_LEVEL = 1;
    public static final int TWO_WINNING_LEVEL = 2;
    public static final int THREE_WINNING_LEVEL = 3;

    public static final double FIRST_PRIZE = 6;
    public static final double TWO_PRIZE = 3;
    public static final double THREE_PRIZE = 1.4;

    public static final int AWARD_YES = 1;
    public static final int AWARD_NO = 0;

    public static final List<List<Integer>>  firstWinningLevelGroup;
    public static final List<List<Integer>>  twoWinningLevelGroup;
    public static final List<List<Integer>>  threeWinningLevelGroup;
    public static final List<List<Integer>>  NoWinningLevelGroup;
    static {
        List<List<Integer>> tempGroup = new ArrayList<>();
        tempGroup.add(Arrays.asList(7,7,7,8,9,9,8,8,9));
        tempGroup.add(Arrays.asList(7,8,7,7,8,9,9,8,9));
        tempGroup.add(Arrays.asList(7,9,9,7,7,8,8,9,8));
        tempGroup.add(Arrays.asList(7,8,7,9,7,9,8,9,8));
        tempGroup.add(Arrays.asList(7,9,8,7,7,9,9,8,8));
        tempGroup.add(Arrays.asList(7,7,8,7,9,9,9,8,8));
        tempGroup.add(Arrays.asList(7,7,9,9,7,8,9,8,8));
        tempGroup.add(Arrays.asList(8,9,7,7,7,9,9,8,8));
        tempGroup.add(Arrays.asList(7,9,9,7,7,9,8,8,8));
        tempGroup.add(Arrays.asList(7,8,8,7,7,9,9,9,8));
        firstWinningLevelGroup = tempGroup;

        tempGroup = new ArrayList<>();
        tempGroup.add(Arrays.asList(8,8,8,9,7,7,9,9,7));
        tempGroup.add(Arrays.asList(8,9,8,8,9,7,7,9,7));
        tempGroup.add(Arrays.asList(8,7,7,8,8,9,9,7,9));
        tempGroup.add(Arrays.asList(8,9,8,7,8,7,9,7,9));
        tempGroup.add(Arrays.asList(8,7,9,8,8,7,7,9,9));
        tempGroup.add(Arrays.asList(8,8,9,8,7,7,7,9,9));
        tempGroup.add(Arrays.asList(8,8,7,7,8,9,7,9,9));
        tempGroup.add(Arrays.asList(9,7,8,8,8,7,7,9,9));
        tempGroup.add(Arrays.asList(8,7,7,8,8,7,9,9,9));
        tempGroup.add(Arrays.asList(8,9,9,8,8,7,7,7,9));
        twoWinningLevelGroup = tempGroup;

        tempGroup = new ArrayList<>();
        tempGroup.add(Arrays.asList(9,9,9,7,8,8,7,7,8));
        tempGroup.add(Arrays.asList(9,7,9,9,7,8,8,7,8));
        tempGroup.add(Arrays.asList(9,8,8,9,9,7,7,8,7));
        tempGroup.add(Arrays.asList(9,7,9,8,9,8,7,8,7));
        tempGroup.add(Arrays.asList(9,8,7,9,9,8,8,7,7));
        tempGroup.add(Arrays.asList(9,9,7,9,8,8,8,7,7));
        tempGroup.add(Arrays.asList(9,9,8,8,9,7,8,7,7));
        tempGroup.add(Arrays.asList(7,8,9,9,9,8,8,7,7));
        tempGroup.add(Arrays.asList(9,8,8,9,9,8,7,7,7));
        tempGroup.add(Arrays.asList(9,7,7,9,9,8,8,8,7));
        threeWinningLevelGroup = tempGroup;

        tempGroup = new ArrayList<>();
        tempGroup.add(Arrays.asList(9,9,7,7,8,8,7,9,8));
        tempGroup.add(Arrays.asList(9,7,9,8,7,8,9,7,8));
        tempGroup.add(Arrays.asList(9,8,8,7,9,7,9,8,7));
        tempGroup.add(Arrays.asList(9,7,9,8,7,8,7,8,9));
        tempGroup.add(Arrays.asList(9,8,7,9,8,9,8,7,7));
        tempGroup.add(Arrays.asList(9,8,7,9,8,9,8,7,7));
        tempGroup.add(Arrays.asList(7,9,8,8,9,7,8,7,9));
        tempGroup.add(Arrays.asList(7,8,9,8,9,9,8,7,7));
        tempGroup.add(Arrays.asList(9,8,8,7,9,8,9,7,7));
        tempGroup.add(Arrays.asList(8,7,7,9,9,9,8,8,7));
        NoWinningLevelGroup = tempGroup;
    }
    // ===========================================================
    // Fields
    // ===========================================================
    public long   orderId; //订单id
    public long   userId;   //用户id
    public int    amount;   //金额
    public int    winningLevel; //几等奖
    public double odds;         //赔率
    public int    bonus;        //返奖金额
    public transient int    award;        //是否领奖 1 是 0否
    public transient String   result;       //翻牌结果7|7|7|8|8|8|9|9|9 777一等奖 888二等奖 999三等奖
    public String  createTime;

    public List<Integer> sort;
    // ===========================================================
    // Constructors
    // ===========================================================
    public BetOrder(){
        this.sort = new ArrayList<>();
    }
    public BetOrder(long userId,int amount,int winningLevel,double odds,int award){
        this.orderId = Constants.generateOrderID();
        this.userId = userId;
        this.amount = amount;
        this.winningLevel = winningLevel;
        this.odds = odds;
        this.bonus = (int)((amount * odds));
        this.award = award;
        this.createTime = DateUtil.getDateTimeString();
        this.buildSort();
    }

    public BetOrder(long userId,int amount){
        this.orderId = Constants.generateOrderID();
        this.userId = userId;
        this.amount = amount;
        this.winningLevel = 0;
        this.odds = 0;
        this.bonus = 0;
        this.award = BetOrder.AWARD_YES;
        this.createTime = DateUtil.getDateTimeString();
        this.buildSort();
    }
    // ===========================================================
    // Getter &amp; Setter
    // ===========================================================

    public long getOrderId() {
        return orderId;
    }

    public long getUserId() {
        return userId;
    }

    public int getAmount() {
        return amount;
    }

    public int getWinningLevel() {
        return winningLevel;
    }

    public double getOdds() {
        return odds;
    }

    public int getBonus() {
        return bonus;
    }

    public int getAward() {
        return award;
    }

    public String getResult() {
        return result;
    }

    public String getCreateTime() {
        return createTime;
    }

    public List<Integer> getSort() {
        return sort;
    }

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    // ===========================================================
    // Methods
    // ===========================================================
    private void buildSort(){
        int number = MathUtils.random(10);
        if(this.winningLevel == FIRST_WINNING_LEVEL){//一等奖
            this.sort = firstWinningLevelGroup.get(number);
        }else if(this.winningLevel == TWO_WINNING_LEVEL){//二等奖
            this.sort = twoWinningLevelGroup.get(number);
        }else if(this.winningLevel == THREE_WINNING_LEVEL){//三等奖
            this.sort = threeWinningLevelGroup.get(number);
        }else{//未中奖
            this.sort = NoWinningLevelGroup.get(number);
        }
        this.result = Joiner.on("|").join(this.sort);
    }
    public void fillSort(){
        if(!StringUtil.isEmpty(this.result)){
            Iterable<String> iterable = Splitter.on("|").split(this.result);
            iterable.forEach(number -> {
                this.sort.add(Integer.parseInt(number));
            });
        }
    }
    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}
